#############
#MAP EMPIRES#
#############
#This to obtain:
#Figure_A16a.jpg
#Figure_A16b.jpg
#Figure_A16c.jpg

rm(list = ls())
library("sf")
library("ggplot2")
library("dplyr")
library("ggrepel")
library("RColorBrewer")
library("lemon")
library("maptools")
library("ggsn")
library("ggplot2")
library("dplyr")
library("lemon")
library("raster")
library("ggrepel")


setwd("/Users/bgpopescu/")
#setwd("C:/Users/bogdanp/")

###########
#1538 MAPS#
###########
#Reading polygons
Ottoman_1552 <- st_read(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb",
                    layer="Ottoman_1552")
sf::sf_use_s2(FALSE)
Ottoman_1552<-st_simplify(Ottoman_1552,  dTolerance = 0.005)

Hungary_Bohemia_1552 <- st_read(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb",
                  layer="Hungary_Bohemia_1552")
# reproject data
Hungary_Bohemia_1552 = st_transform(Hungary_Bohemia_1552, st_crs(Ottoman_1552))
Hungary_Bohemia_1552<-st_simplify(Hungary_Bohemia_1552,  dTolerance = 0.005)

Venice_1552 <- st_read(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb",
                                layer="Venice_1540")
# reproject data
Venice_1552 = st_transform(Venice_1552, st_crs(Ottoman_1552))
Venice_1552<-st_simplify(Venice_1552,  dTolerance = 0.005)

frontier_1538 <- st_read(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb",
                          layer="frontier_1538")
# reproject data
frontier_1538 = st_transform(frontier_1538, st_crs(Ottoman_1552))
frontier_1538<-st_simplify(frontier_1538,  dTolerance = 0.005)
frontier_1538$lon<-sf::st_coordinates(st_centroid(frontier_1538))[,1]
frontier_1538$lat<-sf::st_coordinates(st_centroid(frontier_1538))[,2]


all_coun <- st_read(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb",
                    layer="all_countries_GCS")
all_coun<-st_simplify(all_coun,  dTolerance = 0.005)


#Reading rasters
euro_raster <- raster("./Dropbox/Legacies_Project/Analysis/data/euro_raster.tif")

max_lon<-max(52.669853) + 4
min_lon<-min(34.815009) - 4

max_lat<-max(40.447201) + 4
min_lat<-min(7.556495) - 4

#e <- extent(-160, 10, 30, 60)
e <- extent(min_lat, max_lat, min_lon, max_lon)
rc <- crop(euro_raster, e)	
res(rc)

#Lower resolution raster
rc.aggregate <- aggregate(rc, fact=2)
res(rc.aggregate)

#Get rid of really high mountains
rc <- reclassify(rc.aggregate, c(10000,Inf,NA))
rm(rc.aggregate)
rna <- reclassify(rc, cbind(NA, 0))
rm(rc)
#Convert rasters TO dataframes for plotting with ggplot
hdf <- rasterToPoints(rna); hdf <- data.frame(hdf)
colnames(hdf) <- c("X","Y","Hill")

#   Create vectors for colour breaks
b.hs <- seq(min(hdf$Hill),max(hdf$Hill),length.out=100)

lbl <- frontier_1538
lbl<-subset(frontier_1538, select = c(name, lon, lat))

lbl$name[lbl$name=="Captaincy between Danube and Balaton"]<-"Captaincy between \nDanube and Balaton"
lbl$name[lbl$name=="Captaincy between Balaton and Drava"]<-"Captaincy between \nBalaton and Drava"
lbl$name[lbl$name=="Captaincy of Upper Hungary"]<-"Captaincy of \nUpper Hungary"
lbl$name[lbl$name=="Captaincy of Minining Towns"]<-"Captaincy of \nMining Towns"
lbl$name[lbl$name=="Slavonian Captaincy"]<-"Slavonian \nCaptaincy"
lbl$name[lbl$name=="Croatian Captaincy"]<-"Croatian \nCaptaincy"


cols.fill = c("Ottoman \nTerritory" = alpha("blue", 0.5), 
              "Habsburg \nCivilian Territory" = alpha("pink", 0.5), 
              "Habsburg \nMilitary Territory" = alpha("red", 0.5),
              "Venetian \nTerritory" = alpha("yellow", 0.5))

map<-ggplot()+
  #Raster works but takes a long time
  geom_raster(data=hdf,aes(X,Y,alpha=Hill)) +
  scale_alpha(name = "Altitude", guide="none")  + 
  geom_sf(data = all_coun, fill=NA, color = "black", linewidth = 0.3)+
  geom_sf(data = Ottoman_1552,
          aes(fill = "Ottoman \nTerritory"), color = "grey20", linewidth = 0.6)+
  geom_sf(data = Hungary_Bohemia_1552,
          aes(fill = "Habsburg \nCivilian Territory"), color = "grey20", linewidth = 0.6)+
  geom_sf(data = frontier_1538, 
          aes(fill = "Habsburg \nMilitary Territory"), color = "grey20", linewidth = 0.6)+
  geom_sf(data = Venice_1552,
          aes(fill = "Venetian \nTerritory"), color = "grey20", linewidth = 0.6)+
  scale_fill_manual(name = "Territory", values = cols.fill) +
  coord_sf(ylim=c(34.815009,52.669853), xlim=c(7.556495, 40.447201))+
  theme(legend.position="left")+
  theme_bw()+
  labs(x = "Longitude", y="Latitude")+
  ggtitle("Ottoman - Habsburg Border in 1550s")+
  theme(axis.text.x = element_text(size=14),
        axis.text.y = element_text(size=14),
        axis.title=element_text(size=14),
        plot.title = element_text(hjust = 0.5),
        legend.position = c(1, 0),
        legend.box.background = element_rect(fill='white'),
        legend.background = element_blank(),
        legend.text=element_text(size=12))+
        ggsn::scalebar(x.min = 38, x.max = 40,
           #Above you mention how long the scalebar should be
           y.min = 50.5, y.max = 52,
           #Above you mention how thick the scalebar should be
           dist = 250, dist_unit = "km",
           transform = T, model = "WGS84",
           location = "topright",
           st.size = 4,
           #Above you have the font size of the numbers below the scalebar
           st.dist =0.2,
           #Above you have the distance between the bar and the text, as a proportion of the y axis.
           height=0.2)
# #Above you have anumber between 0 and 1 to indicate the height 
# #of the scale bar, as a proportion of the y axis

map<-reposition_legend(map, 'bottom left')

ggsave(map,file="./Dropbox/Legacies_Project/Paper/graphs/Figure_A16a.jpg", 
       height=19.42, width=20.2, units = "cm", dpi=300)

###########
#1699 Maps#
###########
#Reading polygons
Ottoman_Karlowitz_1699 <- st_read(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb",
                                  layer="T1699_Ottoman_Karlowitz")
Ottoman_Karlowitz_1699<-st_simplify(Ottoman_Karlowitz_1699,  dTolerance = 0.005)

Habsburg_1699 <- st_read(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb",
                         layer="T1699_Habsburg")
Habsburg_1699 <- st_transform(Habsburg_1699, crs(Ottoman_Karlowitz_1699))
Habsburg_1699<-st_simplify(Habsburg_1699,  dTolerance = 0.005)


Venice_1699 <- st_read(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb",
                       layer="T1699_Venice")
Venice_1699 <- st_transform(Venice_1699, crs(Ottoman_Karlowitz_1699))
Venice_1699<-st_simplify(Venice_1699,  dTolerance = 0.005)

krajna_polygon_divisions_1699_1718 <- st_read(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb",
                                              layer="krajna_polygon_divisions_1699_1718")
krajna_polygon_divisions_1699_1718 <- st_transform(krajna_polygon_divisions_1699_1718, crs(Ottoman_Karlowitz_1699))
krajna_polygon_divisions_1699_1718<-st_simplify(krajna_polygon_divisions_1699_1718,  dTolerance = 0.005)


all_coun <- st_read(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb",
                    layer="all_countries_GCS")
all_coun<-st_simplify(all_coun,  dTolerance = 0.005)

krajna_polygon_divisions_1699_1718$lon<-sf::st_coordinates(st_centroid(krajna_polygon_divisions_1699_1718))[,1]
krajna_polygon_divisions_1699_1718$lat<-sf::st_coordinates(st_centroid(krajna_polygon_divisions_1699_1718))[,2]

lbl <- krajna_polygon_divisions_1699_1718

cols.fill = c("Ottoman \nTerritory" = alpha("blue", 0.5), 
              "Habsburg \nCivilian Territory" = alpha("pink", 0.5), 
              "Habsburg \nMilitary Territory" = alpha("red", 0.5),
              "Venetian \nTerritory" = alpha("yellow", 0.5))

map<-ggplot()+
  geom_raster(data=hdf,aes(X,Y,alpha=Hill)) +
  scale_alpha(name = "Altitude in m", guide="none")  + 
  geom_sf(data = all_coun, fill=NA, color = "black", linewidth = 0.4)+
  geom_sf(data = Ottoman_Karlowitz_1699, 
          aes(fill = "Ottoman \nTerritory"), color = "grey20", linewidth = 0.6)+
  geom_sf(data = Habsburg_1699,  
          aes(fill = "Habsburg \nCivilian Territory"), color = "grey20", linewidth = 0.6)+
  geom_sf(data = krajna_polygon_divisions_1699_1718, 
          aes(fill = "Habsburg \nMilitary Territory"), color = "grey20", linewidth = 0.4)+
  geom_sf(data = Venice_1699,  
          aes(fill = "Venetian \nTerritory"), color = "grey20", linewidth = 0.4)+
  scale_fill_manual(name = "Territory", values = cols.fill) +
  scale_colour_manual(values=c("black", "grey60", "grey60", "grey60", "grey60"), guide = "none")+
  coord_sf(ylim=c(34.815009,52.669853), xlim=c(7.556495, 40.447201))+
  theme(legend.position="left")+
  theme_bw()+
  labs(x = "Longitude", y="Latitude")+
  ggtitle("Ottoman - Habsburg Border 1699 - 1718")+
  theme(axis.text.x = element_text(size=14),
        axis.text.y = element_text(size=14),
        axis.title=element_text(size=14),
        plot.title = element_text(hjust = 0.5),
        legend.position = c(1, 0),
        legend.box.background = element_rect(fill='white'),
        legend.background = element_blank(),
        legend.text=element_text(size=12))+
  ggsn::scalebar(x.min = 38, x.max = 40,
                 #Above you mention how long the scalebar should be
                 y.min = 50.5, y.max = 52,
                 #Above you mention how thick the scalebar should be
                 dist = 250, dist_unit = "km",
                 transform = T, model = "WGS84",
                 location = "topright",
                 st.size = 4,
                 #Above you have the font size of the numbers below the scalebar
                 st.dist =0.2,
                 #Above you have the distance between the bar and the text, as a proportion of the y axis.
                 height=0.2)
                 ##Above you have anumber between 0 and 1 to indicate the height 
                 # #of the scale bar, as a proportion of the y axis


map<-reposition_legend(map, 'bottom left')
#map
ggsave(map,file="./Dropbox/Legacies_Project/Paper/graphs/Figure_A16b.jpg", height=19.42, width=20.2, units = "cm", dpi=300)


###########
#1797 Maps#
###########
#Reading polygons
Ottoman_1797 <- st_read(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb",
                        layer="T1797_Ottoman")
Ottoman_1797<-st_simplify(Ottoman_1797,  dTolerance = 0.005)


Habsburg_1797 <- st_read(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb",
                         layer="T1797_Habsburg")

Habsburg_1797 <- st_transform(Habsburg_1797, crs(Ottoman_1797))
Habsburg_1797<-st_simplify(Habsburg_1797,  dTolerance = 0.005)

krajna_polygon_divisions <- st_read(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb",
                                    layer="krajna_polygon_divisions")

krajna_polygon_divisions <- st_transform(krajna_polygon_divisions, crs(Ottoman_1797))
krajna_polygon_divisions<-st_simplify(krajna_polygon_divisions,  dTolerance = 0.005)


all_coun <- st_read(dsn="./Dropbox/Legacies_Project/Analysis/data/data.gdb",
                    layer="all_countries_GCS")
all_coun<-st_simplify(all_coun,  dTolerance = 0.005)



lbl <- krajna_polygon_divisions

lbl$name_regiment[lbl$name_regiment=="German Banat regiment"]<-"German Banat \nregiment"
lbl$name_regiment[lbl$name_regiment=="Vlach Banat regiment"]<-"Vlach Banat \nregiment"
lbl$name_regiment[lbl$name_regiment=="Romanian Banat regiment"]<-"Romanian Banat \nregiment"
lbl$name_regiment[lbl$name_regiment=="Hussar regiment"]<-"Hussar \nregiment"
lbl$name_regiment[lbl$name_regiment=="2nd Szekler regiment"]<-"2nd Szekler \nregiment"
lbl$name_regiment[lbl$name_regiment=="2nd Romanian regiment"]<-"2nd Romanian \nregiment"
lbl$name_regiment[lbl$name_regiment=="Serbian Banat regiment"]<-"Serbian Banat \nregiment"
lbl$name_regiment[lbl$name_regiment=="1st Romanian regiment"]<-"1st Romanian \nregiment"
lbl$name_regiment[lbl$name_regiment=="Lika"]<-""
lbl$name_regiment[lbl$name_regiment=="Ottoschatz"]<-""
lbl$name_regiment[lbl$name_regiment=="Ogulin"]<-""
lbl$name_regiment[lbl$name_regiment=="Sluin"]<-""
lbl$name_regiment[lbl$name_regiment=="1st Banal"]<-""
lbl$name_regiment[lbl$name_regiment=="2nd Banal"]<-""
lbl$name_regiment[lbl$name_regiment=="St. Georgen"]<-""
lbl$name_regiment[lbl$name_regiment=="Kreutz"]<-""
lbl$name_regiment[lbl$name_regiment=="Brod"]<-""
lbl$name_regiment[lbl$name_regiment=="Gradiska"]<-""



cols.fill = c("Ottoman \nTerritory" = alpha("blue", 0.5), 
              "Habsburg \nCivilian Territory" = alpha("pink", 0.5), 
              "Habsburg \nMilitary Territory" = alpha("red", 0.5))



map<-ggplot()+
  geom_raster(data=hdf,aes(X,Y,alpha=Hill)) +
  scale_alpha(name = "Altitude", guide = "none")  + 
  geom_sf(data = all_coun, fill=NA, color = "black", linewidth = 0.4)+
  geom_sf(data = Ottoman_1797, aes(fill = "Ottoman \nTerritory"), color = "grey20", linewidth = 0.6)+
  geom_sf(data = Habsburg_1797, aes(fill = "Habsburg \nCivilian Territory"), color = "grey20", linewidth = 0.6)+
  geom_sf(data = krajna_polygon_divisions, aes(fill = "Habsburg \nMilitary Territory"), color = "grey20", linewidth = 0.6)+
  scale_fill_manual(name = "Territory", values = cols.fill) +
  scale_colour_manual(values=c("black", "grey20", "grey20", "grey20"), guide = "none")+
  coord_sf(ylim=c(34.815009,52.669853), xlim=c(7.556495, 40.447201))+
  theme(legend.position="left")+
  theme_bw()+
  labs(x = "Longitude", y="Latitude")+
  ggtitle("Ottoman - Habsburg Border 1762 - 1873")+
  theme(axis.text.x = element_text(size=14),
        axis.text.y = element_text(size=14),
        axis.title=element_text(size=14),
        plot.title = element_text(hjust = 0.5),
        legend.position = c(1, 0),
        legend.box.background = element_rect(fill='white'),
        legend.background = element_blank(),
        legend.text=element_text(size=12))+
  ggsn::scalebar(x.min = 38, x.max = 40,
                 #Above you mention how long the scalebar should be
                 y.min = 50.5, y.max = 52,
                 #Above you mention how thick the scalebar should be
                 dist = 250, dist_unit = "km",
                 transform = T, model = "WGS84",
                 location = "topright",
                 st.size = 4,
                 #Above you have the font size of the numbers below the scalebar
                 st.dist =0.2,
                 #Above you have the distance between the bar and the text, as a proportion of the y axis.
                 height=0.2)

map<-reposition_legend(map, 'bottom left')

#map
ggsave(map,file="./Dropbox/Legacies_Project/Paper/graphs/Figure_A16c.jpg", 
       height=19.42, width=20.2, units = "cm", dpi=300)



